package com.ruoyi.web.controller.productionre;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.production.domain.PlugReturn;
import com.ruoyi.production.domain.SimpleProduction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.PesticideReturn;
import com.ruoyi.production.service.IPesticideReturnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农药退回Controller
 *
 * @author ruoyi
 * @date 2021-07-17
 */
@Api("农药退回")
@RestController
@RequestMapping("/production/pesticidere")
public class PesticideReturnController extends BaseController
{
    @Autowired
    private IPesticideReturnService pesticideReturnService;

    /**
     * 查询农药退回列表
     */
    @ApiOperation("查询农药退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:list')")
    @GetMapping("/list")
    public TableDataInfo list(PesticideReturn pesticideReturn)
    {
        startPage();
        List<PesticideReturn> list = pesticideReturnService.selectPesticideReturnList(pesticideReturn);
        return getDataTable(list);
    }

    /**
     * 导出农药退回列表
     */
    @ApiOperation("导出农药退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:export')")
    @Log(title = "农药退回", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PesticideReturn pesticideReturn)
    {
        List<PesticideReturn> list=new ArrayList();
        //通过id来取
        Long a[]=pesticideReturn.getPesticideReturnIds();
        for(int i=0;i<a.length;i++) {
            PesticideReturn list1 =pesticideReturnService.selectPesticideReturnById(a[i]);
            list.add(list1);
        }

        ExcelUtil<PesticideReturn> util = new ExcelUtil<PesticideReturn>(PesticideReturn.class);
        return util.exportExcel(list, "农药退回数据");
    }

    /**
     * 获取农药退回详细信息
     */
    @ApiOperation("获取农药退回详细信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:query')")
    @GetMapping(value = "/{pesticideReturnId}")
    public AjaxResult getInfo(@PathVariable("pesticideReturnId") Long pesticideReturnId)
    {
        return AjaxResult.success(pesticideReturnService.selectPesticideReturnById(pesticideReturnId));
    }

    /**
     * 新增农药退回
     */
    @ApiOperation("新增农药退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:add')")
    @Log(title = "农药退回", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PesticideReturn pesticideReturn)
    {
        return toAjax(pesticideReturnService.insertPesticideReturn(pesticideReturn));
    }

    /**
     * 修改农药退回
     */
    @ApiOperation("修改农药退回")
    @ResponseBody
    @PreAuthorize("@ss.hasAnyPermi('production:pesticidere:edit,production:pesticidere:permit')")
    @Log(title = "农药退回", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PesticideReturn pesticideReturn)
    {
        return toAjax(pesticideReturnService.updatePesticideReturn(pesticideReturn));
    }

    /**
     * 删除农药退回
     */
    @ApiOperation("删除农药退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:remove')")
    @Log(title = "农药退回", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pesticideReturnIds}")
    public AjaxResult remove(@PathVariable Long[] pesticideReturnIds)
    {
        return toAjax(pesticideReturnService.deletePesticideReturnByIds(pesticideReturnIds));
    }

    /**
     * 获取农药类型下拉框
     */
    @ApiOperation("获取农药类型下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:pesticidetype')")
    @GetMapping("/pesticidetype")
    public TableDataInfo getPesticideType()
    {
        List<PesticideReturn> list = pesticideReturnService.getPesticideType();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (PesticideReturn pesticideReturn:list) {
            Long id = pesticideReturn.getPesticideTypeId();
            String name = pesticideReturn.getPesticideType();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取农药名称下拉框
     */
    @ApiOperation("获取农药名称下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:pesticidename')")
    @GetMapping("/pesticidename")
    public TableDataInfo getPesticideName()
    {
        List<PesticideReturn> list = pesticideReturnService.getPesticideName();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (PesticideReturn pesticideReturn:list) {
            Long id = pesticideReturn.getPesticideNameId();
            String name = pesticideReturn.getPesticideName();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取用户下拉框
     */
    @ApiOperation("获取用户下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:username')")
    @GetMapping("/username")
    public TableDataInfo getUserName()
    {
        List<PesticideReturn> list = pesticideReturnService.getUserName();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (PesticideReturn pesticideReturn:list) {
            Long id = pesticideReturn.getUserId();
            String name = pesticideReturn.getUserName();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取有效含量下拉框
     */
    @ApiOperation("获取有效含量下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:content')")
    @GetMapping("/content/{pesticideNameId}")
    public TableDataInfo getContent(@PathVariable("pesticideNameId") Long pesticideNameId)
    {
        List<String> list = pesticideReturnService.getContent(pesticideNameId);
        return getDataTable(list);
    }

    /**
     * 获取公司下拉框
     */
    @ApiOperation("获取公司下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:pesticidere:origincompany')")
    @GetMapping("/origincompany/{pesticideNameId}")
    public TableDataInfo getOriginCompany(@PathVariable("pesticideNameId") Long pesticideNameId)
    {
        List<String> list = pesticideReturnService.getOriginCompany(pesticideNameId);
        return getDataTable(list);
    }
}
